import { observable, action, computed, runInAction } from 'mobx'
import { post } from 'lib/net'
import CommonVM from 'cvm/common'
import * as api from 'lib/constants/api'

export default class MyBeanVM extends CommonVM {
  constructor (props) {
    super()
    this.props = props
    this.fetchData()
  }

  // 数据
  @observable data = []
  // 总豆子
  @observable sumBean = 0
  // 剩余豆
  @observable restBean = 0

  // 选中tab
  @observable selectedTab = 'earn'

  // 获得绿豆style
  @computed get earnStyle () {
    return this.selectedTab === 'earn'
    ? 'flex-1 green-color'
    : 'flex-1'
  }
  // 减豆style
  @computed get cutStyle () {
    return this.selectedTab === 'cut'
    ? 'flex-1 green-color'
    : 'flex-1'
  }

  // 选中tab
  @action handleSelect = tab => {
    this.selectedTab = tab
    this.pageInfo.page = 1
    this.fetchData()
  }

  // 加载更多
  @action handleLoadMore = () => {
    console.log(111)
    this.fetchData()
  }

  // 获取我的绿豆详情
  @action fetchData = async () => {
    let params = {
      UserInfoID: this.userId,
      Offset: this.pageInfo.page,
      Limit: this.pageInfo.size
    }
    let url = this.selectedTab === 'earn'
    ? api.BEAN_EARN_RECORD
    : api.BEAN_CUT_RECORD
    this.fetchPage()
    let res = await post(url, params)
    if (res.state === 1) {
      runInAction(() => {
        this.sumBean = res.data.Sum
        this.restBean = res.data.AvailableSum
        let data
        if (this.selectedTab === 'earn') {
          data = res.data.positive
        } else {
          data = res.data.negative
        }
        if (this.pageInfo.page === 1) {
          this.data.splice(0, this.data.length, ...data)
        } else {
          this.data.splice(this.data.length, 0, ...data)
        }
        this.receivePage(this.data)
      })
    } else {
      this.receivePage()
    }
  }
}
